// This is the main DLL file.

#include "stdafx.h"
#include "Sensor.h"


namespace TipTepOpenNI {

///CTOR
Sensor::Sensor(openni::VideoStream& s_depth, openni::VideoStream& s_color):
depthStream(s_depth), colorStream(s_color)
{	
	
}

//Retrieve depth and rgb images from sensor
void Sensor::WaitForStreams(Frame^& p_rgbFrame, Frame^& p_depthFrame)
{
	openni::VideoStream** m_streams;
	openni::VideoFrameRef m_depthFrame;
	openni::VideoFrameRef m_colorFrame;

	m_streams = new openni::VideoStream*[2];
	m_streams[0] = &depthStream;
	m_streams[1] = &colorStream;

	int changedIndex;
	openni::Status rc = openni::OpenNI::waitForAnyStream(m_streams, 2, &changedIndex);
	if (rc != ONI_STATUS_OK)
	{
		TipTepLogger::Log::Write("WaitForStreams", "WaitForStreams failed");
		return;
	}

	switch (changedIndex)
	{
	case 0:
		(depthStream).readFrame(&m_depthFrame); break;
	case 1:
		(colorStream).readFrame(&m_colorFrame); break;
	default:
		TipTepLogger::Log::Write("WaitForStreams", "WaitForStreams failed");
	}
	if (m_depthFrame.isValid())
	{
		Frame^ depthFrame = gcnew Frame(m_depthFrame.getStrideInBytes() / sizeof(openni::DepthPixel),m_depthFrame.getWidth(), m_depthFrame.getHeight(), (openni::DepthPixel*)m_depthFrame.getData());		
		p_depthFrame = depthFrame;		

//		nite::Vector<nite::GestureData>* pGestures = m_depthFrame.GetGestures();
		/*for (int i = 0; i < pGestures->Size(); ++i)
		{
			if (pGestures->GetAt(i).GetState() == NITE_GESTURE_STATE_COMPLETED)
			{
				const nite::Point3d& position = pGestures->GetAt(i).GetCurrentPosition();
				printf("Gesture %d at (%f,%f,%f)\n", pGestures->GetAt(i).GetType(), position.GetX(), position.GetY(), position.GetZ());

				m_pHandTracker->StartHandTracking(pGestures->GetAt(i).GetCurrentPosition());
			}
		}*/
	}
}

}